﻿using System.Collections.Generic;
using System.ComponentModel.Composition;
using InvestmentIntelligence.Files;
using InvestmentIntelligence.SchedulingService.Infrastructure;
using InvestmentIntelligence.TradingData.DataLoaders.Csv;
using InvestmentIntelligence.TradingData.DataLoaders.LoadDataProcessor;
using InvestmentIntelligence.TradingData.DataLoaders.PostProcessing;
using InvestmentIntelligence.TradingData.Handlers.Handlers.DataHandlers;
using InvestmentIntelligence.TradingData.Handlers.Mus.FieldsConfiguration;
using InvestmentIntelligence.TradingData.TradingDataIdentityModels;
using InvestmentIntelligence.TradingData.TradingDataModels;

namespace InvestmentIntelligence.TradingData.Handlers.Mus
{
    [Export(typeof(IScheduledTaskFactory))]
    [ExportMetadata("TaskName", "LoadMusCsvSecurities")]
    public partial class CsvIGlgSecurityHandlerFactory : FolderBasedHandlerFactoryBase, IScheduledTaskFactory
    {
        private readonly ScheduledTaskParameter _securitiesFolder = new StringScheduledTaskParameter(
            "securities-dir", "specifies a directory containing security data");

        public override IEnumerable<ScheduledTaskParameter> GetParameters()
        {
            yield return _securitiesFolder;
        }

        public override IScheduledTask CreateTask(CreateTaskParams createTaskParams, IDictionary<ScheduledTaskParameter, string> arguments)
        {            
            var fields = new SecurityFields();
            var files = FilteredFileReader.GetFiles(_securitiesFolder.Get(arguments, ""), "*", "csv", true);

            var securityLoader = new SecurityDataLoader(files, fields, new List<SecurityIdentityModel>());
            var securityDataProcessor = new LoadDataProcessor<TradingDataSecurityDto>(securityLoader, Mappings.TradingDataSecurityDto, new List<IDataPostProcessing<TradingDataSecurityDto>>(), TradingDataSecurityDto.TradingDataSecurityDtoComparer);

            return new DbPopulareSecuritiesHandler(securityDataProcessor, new List<SecurityIdentityModel>());
        }
    }
}